package api

import (
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gconv"
	"shrimp-blog-server/app/model"
	"shrimp-blog-server/app/service"
	"shrimp-blog-server/utils"
	"strings"
)

// 定义接口类型
type (
	apiUser          struct{}
	apiArticle       struct{}
	apiArticleLabel  struct{}
	apiArticleSort   struct{}
	apiComment       struct{}
	apiLabel         struct{}
	apiLink          struct{}
	apiMessages      struct{}
	apiNavbar        struct{}
	apiPageTemplate  struct{}
	apiResource      struct{}
	apiRole          struct{}
	apiRoleResources struct{}
	apiSorts         struct{}
	apiSubject       struct{}
	apiSubjectItem   struct{}
)

// 初始化接口类型
var (
	User          = apiUser{}
	Article       = apiArticle{}
	ArticleLabel  = apiArticleLabel{}
	ArticleSort   = apiArticleSort{}
	Comment       = apiComment{}
	Label         = apiLabel{}
	Link          = apiLink{}
	Messages      = apiMessages{}
	Navbar        = apiNavbar{}
	PageTemplate  = apiPageTemplate{}
	Resource      = apiResource{}
	Role          = apiRole{}
	RoleResources = apiRoleResources{}
	Sorts         = apiSorts{}
	Subject       = apiSubject{}
	SubjectItem   = apiSubjectItem{}
)

// 初始化service
var (
	userService          = new(service.UserService)
	articleService       = new(service.ArticleService)
	articleLabelService  = new(service.ArticleLabelService)
	articleSortService   = new(service.ArticleSortService)
	commentService       = new(service.CommentService)
	labelService         = new(service.LabelService)
	linkService          = new(service.LinkService)
	messageService       = new(service.MessagesService)
	navbarService        = new(service.NavbarService)
	pageTemplateService  = new(service.PageTemplateService)
	resourceService      = new(service.ResourceService)
	roleService          = new(service.RoleService)
	roleResourcesService = new(service.RoleResourcesService)
	sortService          = new(service.SortsService)
	subjectService       = new(service.SubjectService)
	subjectItemService   = new(service.SubjectItemService)
)

var responses = utils.Response

// 根据key 为id 获取对应的value
func getQueryParamID(req *ghttp.Request) []uint {
	query := req.Get("id")
	if query == nil {
		return make([]uint, 0)
	}
	idStr := strings.Split(gconv.String(query), ",")
	ids := make([]uint, len(idStr))
	for i, v := range idStr {
		ids[i] = gconv.Uint(v)
	}
	return ids
}

func getPage(request *ghttp.Request) (page model.Page) {
	err := request.Parse(&page)
	if err != nil {
		panic(err)
	}
	if page.Size == 0 {
		page.Size = 10
	}
	if page.Page == 0 {
		page.Page = 1
	}
	return
}
